/****************************************************************************
 **
 ** Copyright (C) 2004-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License versions 2.0 or 3.0 as published by the Free Software
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
 ** included in the packaging of this file.  Alternatively you may (at
 ** your option) use any later version of the GNU General Public
 ** License if such license has been publicly approved by Trolltech ASA
 ** (or its successors, if any) and the KDE Free Qt Foundation. In
 ** addition, as a special exception, Trolltech gives you certain
 ** additional rights. These rights are described in the Trolltech GPL
 ** Exception version 1.2, which can be found at
 ** http://www.trolltech.com/products/qt/gplexception/ and in the file
 ** GPL_EXCEPTION.txt in this package.
 **
 ** Please review the following information to ensure GNU General
 ** Public Licensing requirements will be met:
 ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
 ** you are unsure which license is appropriate for your use, please
 ** review the following information:
 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
 ** or contact the sales department at sales@trolltech.com.
 **
 ** In addition, as a special exception, Trolltech, as the sole
 ** copyright holder for Qt Designer, grants users of the Qt/Eclipse
 ** Integration plug-in the right for the Qt/Eclipse Integration to
 ** link to functionality provided by Qt Designer and its related
 ** libraries.
 **
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
 ** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

#include <QtGui>
#include <iostream>

#include "keyconfig.h"

struct KeyConfig::KeyCodeMapEntry KeyConfig::qtKeyMap[KeyConfig::NumKeycodes]
    = {
          { "A", Qt::Key_A },
          { "B", Qt::Key_B },
          { "C", Qt::Key_C },
          { "D", Qt::Key_D },
          { "E", Qt::Key_E },
          { "F", Qt::Key_F },
          { "G", Qt::Key_G },
          { "H", Qt::Key_H },
          { "I", Qt::Key_I },
          { "J", Qt::Key_J },
          { "K", Qt::Key_K },
          { "L", Qt::Key_L },
          { "M", Qt::Key_M },
          { "N", Qt::Key_N },
          { "O", Qt::Key_O },
          { "P", Qt::Key_P },
          { "Q", Qt::Key_Q },
          { "R", Qt::Key_R },
          { "S", Qt::Key_S },
          { "T", Qt::Key_T },
          { "U", Qt::Key_U },
          { "V", Qt::Key_V },
          { "W", Qt::Key_W },
          { "X", Qt::Key_X },
          { "Y", Qt::Key_Y },
          { "Z", Qt::Key_Z },
          { "0", Qt::Key_0 },
          { "1", Qt::Key_1 },
          { "2", Qt::Key_2 },
          { "3", Qt::Key_3 },
          { "4", Qt::Key_4 },
          { "5", Qt::Key_5 },
          { "6", Qt::Key_6 },
          { "7", Qt::Key_7 },
          { "8", Qt::Key_8 },
          { "9", Qt::Key_9 },
          { "Left", Qt::Key_Left },
          { "Right", Qt::Key_Right },
          { "Up", Qt::Key_Up },
          { "Down", Qt::Key_Down },
          { "Space", Qt::Key_Space }
};

KeyConfigDialog::KeyConfigDialog(QWidget *parent, Qt::WindowFlags f)
    : QDialog(parent, f)
{
    okButton = new QPushButton(tr("&Ok"));
    okButton->setFocusPolicy(Qt::NoFocus);
    okButton->setDefault(true);
    cancelButton = new QPushButton(tr("&Cancel"));
    rotateComboBox = new QComboBox;
    leftComboBox = new QComboBox;
    rightComboBox = new QComboBox;
    fallComboBox = new QComboBox;

    for (int i = 0; i < KeyConfig::NumKeycodes; ++i) {
        rotateComboBox->addItem(tr(KeyConfig::qtKeyMap[i].name));
        leftComboBox->addItem(tr(KeyConfig::qtKeyMap[i].name));
        rightComboBox->addItem(tr(KeyConfig::qtKeyMap[i].name));
        fallComboBox->addItem(tr(KeyConfig::qtKeyMap[i].name));
    }

    connect(okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(createLabel(tr("Rotate:")), 0, 0);
    layout->addWidget(createLabel(tr("Left:")), 1, 0);
    layout->addWidget(createLabel(tr("Right:")), 2, 0);
    layout->addWidget(createLabel(tr("Fall:")), 3, 0);
    layout->addWidget(cancelButton, 4, 0);

    layout->addWidget(rotateComboBox, 0, 1);
    layout->addWidget(leftComboBox, 1, 1);
    layout->addWidget(rightComboBox, 2, 1);
    layout->addWidget(fallComboBox, 3, 1);
    layout->addWidget(okButton, 4, 1);

    setLayout(layout);

    setWindowTitle(tr("Key Configuration"));
    resize(100, 100);

    rotate = KeyConfig::Key_Up;
    left = KeyConfig::Key_Left;
    right = KeyConfig::Key_Right;
    fall = KeyConfig::Key_Space;
}

void KeyConfigDialog::showEvent(QShowEvent *event)
{
    rotateComboBox->setCurrentIndex(rotate);
    leftComboBox->setCurrentIndex(left);
    rightComboBox->setCurrentIndex(right);
    fallComboBox->setCurrentIndex(fall);
}

void KeyConfigDialog::okButtonClicked()
{
    rotate = (KeyConfig::KeyCode)rotateComboBox->currentIndex();
    left = (KeyConfig::KeyCode)leftComboBox->currentIndex();
    right = (KeyConfig::KeyCode)rightComboBox->currentIndex();
    fall = (KeyConfig::KeyCode)fallComboBox->currentIndex();

    accept();
}

void KeyConfigDialog::cancelButtonClicked()
{
    rotateComboBox->setCurrentIndex(rotate);
    leftComboBox->setCurrentIndex(left);
    rightComboBox->setCurrentIndex(right);
    fallComboBox->setCurrentIndex(fall);

    reject();
}

QLabel *KeyConfigDialog::createLabel(const QString &text)
{
    QLabel *lbl = new QLabel(text);
    lbl->setAlignment(Qt::AlignRight | Qt::AlignBottom);
    return lbl;
}
